/*
 * @Author: your name
 * @Date: 2021-02-27 23:28:16
 * @LastEditTime: 2021-07-22 16:57:27
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue3-antd-koa\koa-api\src\middlewares\handler\error.js
 */
/**
 * @description 这里使用到了高阶函数
 * @type {Custom.IMiddlewareHandler}
 */
const errorHandler = (options = {}) => {
    return async (ctx, next) => {
        try {
            await next();
            const { body, status } = ctx;
            const isNotFound = status === 404 && !body
            if (isNotFound) {
                ctx.success('资源未找到', status)
            }
        } catch (error) {
            let { status = 500, message } = error;
            /**
             * 设置错误返回的状态码
             */
            const parameterErrorKeywords = [
                'body', 'query', 'param'
            ]
            const isParameterError = parameterErrorKeywords
                .some(keyword => message.includes(keyword))
            status = isParameterError ? 400 : status
            //  处理token出错
            const isAuthenticationError =
                message === "Authentication Error"

            if (isAuthenticationError) {
                message = "用户未登录, 请先登录"
            }
            const isProd = ctx.app.env === 'production'
            const isServerError = status === 500
            const isProdServerError = isProd && isServerError
            const displayableError = [
                isParameterError,
                isAuthenticationError
            ].includes(true)

            if (!displayableError && isProdServerError) {
                ctx.app.emit("error", { message, status })
                message = "服务端错误"
            }
            ctx.fail(message, status)
            ctx.app.emit("error", { message, status })
        }
    }
}

module.exports = errorHandler


// const errorHandler = (options = {}) => {
//     return async (ctx, next) => {
//         try {
//             await next()
//             const { body, status } = ctx
//             const isNotFound = status === 404 && !body
//             if (isNotFound) {
//                 ctx.success("资源未找到", status)
//             }
//         } catch (error) {
//             let { status = 500, message } = error
//             /**
//              * 设置返回的状态码
//              */
//             const parameterErrorKeywords = [
//                 "body",
//                 "query",
//                 "param"
//             ]
//             const isParameterError = parameterErrorKeywords.some(keyword => message.includes(keyword))
//             status = isParameterError ? 400 : status

//             const isProd = ctx.app.env === "production"
//             const isServerError = status === 500
//             const isProdServerError = isProd && isServerError
//             if (isProdServerError) {
//                 message = "服务端错误"
//             }
//             ctx.fail(message, status)
//             ctx.app.emit("error", { message, status })
//         }
//     }
// }

// module.exports = errorHandler